import pygame, sys, math, random
from MainCharacter import Player
from Background import Background
from Wall import Wall
from Powerup import Powerup
from Enemy import BlackBallEnemy
from Darkness import Darkness
from Door import Door

clock = pygame.time.Clock()

width = 900
height = 700
size = width, height
screen = pygame.display.set_mode(size)

startscreen = pygame.image.load("rsc/Backgrounds/startscreen.png")
startscreen = pygame.transform.scale(startscreen,size)
startrect = startscreen.get_rect()                                                                                                                                                                                                                                                                                                                                                      
player = Player("rsc/Characters/Super Spyy.png", "rsc/Characters/Super Spyy - Shield.png", (10, 350))
#background = Background("level one.png")
enemy1 = BlackBallEnemy("rsc/Characters/enemy.png")
enemy2 = BlackBallEnemy("rsc/Characters/enemy.png", (100,100), (4,4), (150,150))
enemy3 = BlackBallEnemy("rsc/Characters/enemy.png", (100,100), (3,3), (300,300))

dark = Darkness("rsc/Backgrounds/smalllight.png")
door = Door("rsc/Power Ups/Door.png", (875, 450))

bgColor = r,g,b = 0,0,0

powerUps = []
powerUpTimeMax = 50
powerUpTime = powerUpTimeMax

start = False

level = [Wall(0,0,90, 'h'),     #Outside Border
         Wall(0,0,30, 'v'),
         Wall(0,40,30, 'v'),
         Wall(0,69,90, 'h'),
         Wall(89,0,40, 'v'),
         Wall(89,50,20, 'v'),
         
         Wall(0,30,50, 'h'),    #Inner Walls
         Wall(0,50,10, 'v'),
         Wall(0,40,30, 'h'),
         Wall(40,40,20, 'v'),
         Wall(30,40,20, 'v'),
         Wall(10,60,21, 'h'),
         Wall(60,20,30, 'v'),
         Wall(50,30,20, 'v'),
         Wall(40,60,40, 'h'),
         Wall(10,20,60, 'h'),
         Wall(70,20,30, 'v'),
         Wall(80,50,10, 'h'),
         Wall(80,50,11, 'v'),
         Wall(80,10,40, 'v'),
         Wall(60,10,20, 'h'),
         Wall(60,10,10, 'v'),
         Wall(10,10,10, 'v'),
         Wall(20,0,10, 'v'),
         Wall(30,10,10, 'v'),
         Wall(40,0,10, 'v'),
         Wall(50,0,10, 'v')]

while True:
    while not start:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    start = True
        
        screen.blit(startscreen, startrect)
        pygame.display.flip()
        clock.tick(60)
    while start and player.alive:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    player.direction("right")
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    player.direction("left")
                if event.key == pygame.K_w or event.key == pygame.K_UP:
                    player.direction("up")
                if event.key == pygame.K_s or event.key == pygame.K_DOWN:
                    player.direction("down")
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    player.direction("stop right")
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    player.direction("stop left")
                if event.key == pygame.K_w or event.key == pygame.K_UP:
                    player.direction("stop up")
                if event.key == pygame.K_s or event.key == pygame.K_DOWN:
                    player.direction("stop down")
        
        #print powerUpTime
        if powerUpTime >= 10:
            powerUpTime -= 1
        else:
            powerUpTime = powerUpTimeMax
            powerUps += [Powerup([random.randint(25, width-25), random.randint(25, height-25)])]
        
        player.update()
        
        #print player.speed
        for powerUp in powerUps:
            powerUp.update()
            
        player.outerwallhit(900, 700)
        player.enemyhit(enemy1)
        player.enemyhit(enemy2)
        player.enemyhit(enemy3)
        enemy1.update(player)
        door.collidePlayer(player)
        dark.move(player)
        player.outerwallhit(1200, 700)    
        for powerUp in powerUps:
            powerUp.collidePlayer(player, dark)
            powerUp.collideEnemy(enemy1)
            powerUp.collideEnemy(enemy2)
            powerUp.collideEnemy(enemy3)
        
        for powerUp in powerUps: 
            if not powerUp.living:
                powerUps.remove(powerUp)
        
        enemy1.update(player)
        enemy2.update(player)
        enemy3.update(player)
        player.outerwallhit(900, 700)
        screen.fill(bgColor)
        for wall in level:
            #print str(wall)
            screen.blit(wall.image, wall.rect)
            wall.collide(player)
        #print "----------------------------------"
        #screen.blit(background.image, background.rect)
        for powerUp in powerUps:
            screen.blit(powerUp.image, powerUp.rect)
        screen.blit(player.image, player.rect)
        screen.blit(enemy1.image, enemy1.rect)
        screen.blit(enemy2.image, enemy2.rect)
        screen.blit(enemy3.image, enemy3.rect)
        screen.blit(door.image, door.rect)
        screen.blit(dark.image, dark.rect)
        
        if door.living == False:
            winscreen = pygame.image.load("rsc/Backgrounds/winner screen.png")
            winscreen = pygame.transform.scale(winscreen,size)
            winrect = winscreen.get_rect() 
            screen.blit(winscreen, winrect)
            pygame.display.flip()
            clock.tick(60)
        
            
        pygame.display.flip()
        clock.tick(60)
        
    while start and not player.alive:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    start = True
                    player = Player("rsc/Characters/Super Spyy.png", "rsc/Characters/Super Spyy - Shield.png", (10, 350))
                    enemy1 = BlackBallEnemy("rsc/Characters/enemy.png")
                    enemy2 = BlackBallEnemy("rsc/Characters/enemy.png", (100,100), (4,4), (150,150))
                    enemy3 = BlackBallEnemy("rsc/Characters/enemy.png", (100,100), (3,3), (300,300))
                    dark = Darkness("rsc/Backgrounds/smalllight.png")
                                        
        deathscreen = pygame.image.load("rsc/Backgrounds/death screen.png")
        deathscreen = pygame.transform.scale(deathscreen,size)
        deathrect = deathscreen.get_rect() 
        screen.blit(deathscreen, deathrect)
        pygame.display.flip()
        clock.tick(60)
                    